﻿using System;
using System.Data;
using System.Configuration;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Pub;

namespace JCNT.DAL
{
    public class CTree
    {
        CADO ado1 = new CADO();

        public void Fill_Tree(TreeView TreeView1)
        {
            string s_sql = "Select a.id,tit,a.lab from wp_note a left join wp_nlab b on a.lab=b.lab "
                + "Where b.vis=0 order by a.lab,a.tit";

            //s_sql = "SELECT id, tit, lab FROM wp_note ORDER BY Lab, Tit";
            DataSet ds1 = ado1.sel1(s_sql);

            if (ado1.ds_count > 0)
            {
                TreeNode n1 = null;
                TreeNode n2 = null;
                TreeView1.Nodes.Clear();
                DataTable dt1 = ds1.Tables[0];
                string s_id, s_tit, s_lab, s_lab2;
                for (int i = 0; i < dt1.Rows.Count; i++)
                {

                    s_id = dt1.Rows[i]["id"].ToString();
                    s_tit = dt1.Rows[i]["tit"].ToString();
                    s_lab = dt1.Rows[i]["lab"].ToString();
                    if (i > 0)
                        s_lab2 = dt1.Rows[i - 1]["lab"].ToString();
                    else
                        s_lab2 = "";
                    if (s_lab != s_lab2)
                    {
                        n1 = new TreeNode();
                        n1.Text = s_lab;
                        n1.Value = s_id;
                        //n1.ImageUrl = "../img/gif_1.gif";
                        //n1.ImageUrl = getImg(n1.Value, EptType.JG);
                    }

                    n2 = new TreeNode();
                    n2.Text = s_tit;
                    n2.Value = s_lab;
                    n2.NavigateUrl = "Right.aspx?id=" + s_id;
                    n2.Target = "iframe1";
                    //n2.ImageUrl = getImg(n2.Value, EptType.CT);
                    n1.ChildNodes.Add(n2);
                    if (s_lab != s_lab2)
                    {
                        //n1.Expanded = false;
                        TreeView1.Nodes.Add(n1);
                    }


                    //nod1.ChildNodes.Add(n1);
                }

            }

        }

        public void fillTree(TreeView tv1, TreeNode node, string RootID)
        {
            string strSQL = "Select * From WP_NLab Where vis=1 and RootID='" + RootID + "' Order By Lab";
            DataSet ds1 = ado1.sel1(strSQL);
            //Pub.bugMsbox("RootID=" + RootID.ToString() + " And ds_count=" + ado1.ds_count.ToString());

            foreach (DataRow dr in ds1.Tables[0].Rows)
            {
                TreeNode node1 = new TreeNode();
                string Lab = dr["Lab"].ToString();
                string ID = dr["ID"].ToString();
                node1.Text = Lab;
                node1.Value = ID;
                node1.ImageUrl = @"..\images\fold1.png";

                fillTree(null, node1, ID);

                if (node == null)
                {
                    tv1.Nodes.Add(node1);
                }
                else
                {
                    node.ChildNodes.Add(node1);
                }
            }

            string strSQL2 = "Select ID, Tit From WP_Note Where RootID = " + RootID + " Order By Lab";
            DataSet ds2 = ado1.sel1(strSQL2);
            foreach (DataRow dr2 in ds2.Tables[0].Rows)
            {
                TreeNode node2 = new TreeNode();
                string Tit = dr2["Tit"].ToString();
                string tID = dr2["ID"].ToString();
                node2.Text = Tit;
                node2.Value = tID;
                node2.NavigateUrl = "Right.aspx?id=" + tID;
                node2.Target = "iframe_Center";
                node2.ImageUrl = @"..\images\file.gif";
                if (node != null)
                    node.ChildNodes.Add(node2);
                else
                    tv1.Nodes.Add(node2);
            }




        }


    }
}
